package com.ce.webframework.sec.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ce.webframework.sec.model.SecExcludeSchedDO;
import com.ce.webframework.sec.model.SecScheduleDO;
import com.ce.webframework.sec.service.ISecScheduleService;
import com.ce.webframework.sec.dao.ISecScheduleDao;
import com.sitechasia.webx.core.service.impl.BaseService;

public class SecScheduleServiceImpl extends BaseService implements ISecScheduleService{

		private ISecScheduleDao  secScheduleDao;
	
		public void setSecScheduleDao(ISecScheduleDao secScheduleDao) {
			this.secScheduleDao = secScheduleDao;
		}
	
		/**
		 * 功能描述:增加日程记录
		 *
		 * @param schDO
		 *            SecScheduleDO 实体对象
		 * @return SecScheduleDO  增加后的对象
		 */
	
	
		public SecScheduleDO addSecSchedule(SecScheduleDO schDO) {
			return secScheduleDao.addSecSchedule(schDO);
		}
	
		/**
		 * 功能描述:更新日程记录
		 *
		 * @param calDO
		 *            SecScheduleDO 实体对象
		 * @return schDO
		 *            增加后的对象
		 */
		public SecScheduleDO updateSecSchedule(SecScheduleDO schDO) {
			 return secScheduleDao.updateSecSchedule(schDO);
		}
	
		/**
		 * 功能描述:按主键Id 删除日程记录
		 *
		 * @param scheduleId
		 *            SecScheduleDO 的主键
		 * @return boolean 删除成功是为true，失败时为false
		 */
	
		public boolean deleteSecSchedule(Long scheduleId)  {
		  return secScheduleDao.deleteSecSchedule(scheduleId);
		}
	
	
		/**
		 * 功能描述:根据scheduleId查找SecScheduleDO对象
		 *
		 * @param scheduleId
		 *            SecScheduleDO对象的Id
		 * @return SecScheduleDO  id对应的SecScheduleDO对象
		 */
		public SecScheduleDO getSecScheduleById(Long scheduleId)  {
			return secScheduleDao.getSecScheduleById(scheduleId);
		}
	
		/**
		 * 功能描述:根据scheduleId查找SecScheduleDO对象
		 *
		 * @param scheduleId
		 *            SecScheduleDO对象的Id
		 * @param remindTime
		 *            remindTime
		 * @return SecScheduleDO  id对应的SecScheduleDO对象
		 */
		public SecScheduleDO getSecScheduleById(Long scheduleId,Date remindTime) {
		return	secScheduleDao.getSecScheduleById(scheduleId, remindTime);
		}
	
	
	
		/**
		 * 功能描述:根据用户Id和日期 查找 SecScheduleDO对象列表
		 *
		 * @param userId
		 *            用户的Id
		 * @param selectedDate
		 *            当前选择的日期
		 * @return List<SecScheduleDO> 对象
		 *
		 * 返回符合条件的查询对象列表
		 */
	
		public List<SecScheduleDO> getSecScheduleList(Long userId, Date selectedDate) {
			
			Calendar cal = Calendar.getInstance();
		    cal.setTime(selectedDate);
		    cal.set(Calendar.HOUR_OF_DAY, 0);
		    cal.set(Calendar.MINUTE, 0);
		    cal.set(Calendar.SECOND, 0);
		    cal.set(Calendar.MILLISECOND, 0);
		    Date beginDate = cal.getTime();
		    cal.add(Calendar.DATE, 1);
		    Date endDate = cal.getTime();
		    
		    List<SecScheduleDO> list =secScheduleDao.getSecScheduleList(userId,beginDate,endDate);
	        List<SecScheduleDO> returnList = this.tackleRepeatCycle(list,selectedDate);
			
		    return	returnList;
		}
	
		/**
		 * 功能描述:根据用户传入的日期,记录有数据的日期
		 *
		 * @param userId
		 *            用户的Id
		 * @param selectedDate
		 *            当前日期
		 * @return int[]
		 *
		 * 返回符合条件的查询对象列表
		 */
	
	
		public int[] changeMonthStyle(Long userId,Date selectedDate) {
			Calendar cal = Calendar.getInstance();
		    cal.setTime(selectedDate);
		    cal.set(Calendar.HOUR_OF_DAY, 0);
		    cal.set(Calendar.MINUTE, 0);
		    cal.set(Calendar.SECOND, 0);
		    cal.set(Calendar.MILLISECOND, 0);
		    cal.set(Calendar.DATE, 1);
		    Date beginDate = cal.getTime();
		    cal.roll(Calendar.MONTH,0);
		    cal.roll(Calendar.DATE, -1);
		    cal.set(Calendar.HOUR_OF_DAY, 23);
		    cal.set(Calendar.MINUTE, 59);
		    cal.set(Calendar.SECOND, 59);
		    cal.set(Calendar.MILLISECOND, 999);
		    Date endDate = cal.getTime();
		   
		   List<SecExcludeSchedDO>  listExclude= secScheduleDao.getMonthExclueStyle(userId, beginDate, endDate);
			              
			int[] dateDay = new int[32];
			//用数组列出所有的要排斥的日期的重复次数
			for(int i = 0;i<listExclude.size();i++){
				SecExcludeSchedDO cldDO = listExclude.get(i);
				Calendar cld = Calendar.getInstance();
				cld.setTime(cldDO.getExcludeDate());
				dateDay[cld.get(Calendar.DATE)] =dateDay[cld.get(Calendar.DATE)]- 1;
			}
	
		List<SecScheduleDO> list = secScheduleDao.getMonthHaveStyle(userId, beginDate, endDate);
			//处理周期重复的情况
			this.tackleRepeatMonthCycle(list,beginDate, endDate, dateDay);
	
	     List<Integer>  intList=new ArrayList<Integer>();
	 		for(int i=0;i<=31;i++){
				if(dateDay[i]>0){
					intList.add(i);
				}
			}
	     int sum=intList.size();
	 	 int[] intArray=new int[sum];
	 	 for(int i=0;i<sum;i++){
	 		 intArray[i]=intList.get(i);
	 	 }
	 	 for(int i=0;i<intArray.length;i++){
	 		 System.out.println(" intArray="+intArray[i]);
	 	 }
			return intArray;
		}
	
	
		/**
		 * 功能描述:根据 scheduleId 删除 SecExcludeSchedDO 表的记录
		 * @param scheduleId
		 *           SecScheduleDO 的主键
		 * @return  void
	     */
		public void delSecExcludeSchedDO(Long  scheduleId) {
			secScheduleDao.delSecExcludeSchedDO(scheduleId);
		}
	
	
		/**
		 * 功能描述:增加一条 记录到 日历 循环排除表
		 *
		 * @param SecExcludeSchedDO
		 *            SecExcludeSchedDO 实体对象
		 * @return void
		 */
		public void addSecExcludeSchedDO(SecExcludeSchedDO schExcludeDO) {
			secScheduleDao.addSecExcludeSchedDO(schExcludeDO);
		}
	
	
		
		/**
		 * 功能描述:处理当日循环重复的数据
		 *
		 * @param schList
		 *          待处理的List
		 * @param selectedDate
		 *          选中的当日
		 * @return List<SecScheduleDO>
		 */
		 public List<SecScheduleDO> tackleRepeatCycle(List<SecScheduleDO> schList,Date selectedDate){
			  List<SecScheduleDO> tackleList=new ArrayList<SecScheduleDO>(0);
	
			  Calendar selectedCal=Calendar.getInstance();
			  selectedCal.setTime(selectedDate);
	
			  for(SecScheduleDO schDO:schList){
					//开始时间
					Calendar beginCal=Calendar.getInstance();
					beginCal.setTime(schDO.getStartTime());
	
					//结束时间
					Calendar endCal=Calendar.getInstance();
					if(schDO.getEndTime()!=null){
					endCal.setTime(schDO.getEndTime());
					}
	
					//转换成当日时间
					Calendar nowCal=Calendar.getInstance();
					nowCal.setTime(selectedDate);
					nowCal.set(Calendar.HOUR,beginCal.get(Calendar.HOUR));
					nowCal.set(Calendar.MINUTE,beginCal.get(Calendar.MINUTE));
	
					//不重复或者是每日重复
					if((schDO.getRepeatCycle().equals("31"))||(schDO.getRepeatCycle().equals("32"))){
						schDO.setRemindTime(nowCal.getTime());
						tackleList.add(schDO);
					}
	
				   //每周一次
				   if(schDO.getRepeatCycle().equals("33")
						   &&( beginCal.get(Calendar.DAY_OF_WEEK)==selectedCal.get(Calendar.DAY_OF_WEEK))){
					   schDO.setRemindTime(nowCal.getTime());
						  tackleList.add(schDO);
				   }//end 每周
	
				   //每月一次
				   if(schDO.getRepeatCycle().equals("34")
						   &&( beginCal.get(Calendar.DAY_OF_MONTH)==selectedCal.get(Calendar.DAY_OF_MONTH))){
					   schDO.setRemindTime(nowCal.getTime());
					   tackleList.add(schDO);
				   }//end 每月
	
			  }//end for
			  return  tackleList;
		  }
		
			/**
			 * 功能描述:处理当月循环重复的数据
			 *
			 * @param schList
			 *          待处理的List
			 * @param beginDate
			 *         当月开始时间
			 * @param endDate
			 *         当月结束时间
			 * @param dateDay
			 *         记录的数组
			 * @return List<SecScheduleDO>  dateDay
			 */
		 public List<SecScheduleDO> tackleRepeatMonthCycle(List<SecScheduleDO> schList,
				 Date beginDate,Date endDate,int[] dateDay){
			  List<SecScheduleDO> tackleList=new ArrayList<SecScheduleDO>(0);
	
				//月初
				Calendar monthBegin=Calendar.getInstance();
				monthBegin.setTime(beginDate);
	
				//月末
				Calendar monthEnd=Calendar.getInstance();
				monthEnd.setTime(endDate);
				System.out.println( " schList  size" +schList.size());
			  for(SecScheduleDO schDO:schList){
					//开始时间
					Calendar beginCal=Calendar.getInstance();
					beginCal.setTime(schDO.getStartTime());
					int week=beginCal.get(Calendar.DAY_OF_WEEK);
					int day=beginCal.get(Calendar.DAY_OF_MONTH);
	
					//结束时间
					Calendar endCal=Calendar.getInstance();
					endCal.setTime(schDO.getEndTime());
					endCal.set(Calendar.HOUR_OF_DAY, 23);
					endCal.set(Calendar.MINUTE, 59);
					endCal.set(Calendar.SECOND, 59);
					endCal.set(Calendar.MILLISECOND, 999);
	
					//计算比较后开始时间和结束时间
					if(monthBegin.compareTo(beginCal)>0){
						beginCal=monthBegin;
					}
					if(endCal.compareTo(monthEnd)>0){
						endCal=monthEnd;
					}
	
					//不重复
					if((schDO.getRepeatCycle().equals("31"))){
						dateDay[day]+=1;
					}
	
					//每日重复
					if(schDO.getRepeatCycle().equals("32")){
						while(beginCal.before(endCal)  ){
							dateDay[beginCal.get(Calendar.DATE)]+=1;
							beginCal.add(Calendar.DATE, 1);
						}
					}
	
				   //每周一次
				   if(schDO.getRepeatCycle().equals("33")){
					   while(beginCal.before(endCal)){
						   if(beginCal.get(Calendar.DAY_OF_WEEK)==week){
						   dateDay[beginCal.get(Calendar.DATE)]+=1;
						   }
						   beginCal.add(Calendar.DAY_OF_WEEK,7);
							  }
					   }//end 每周
	
				   //每月一次
				   if(schDO.getRepeatCycle().equals("34")
						   &&(beginCal.get(Calendar.DAY_OF_MONTH)<=day) &&(endCal.get(Calendar.DAY_OF_MONTH)>day)){
					   dateDay[day]+=1;
			   }//end 每月
	
			  }//end for
			  return  tackleList;
		  }
		 
	
			/**
			 * 功能描述:每日定时提醒功能
			 * @param userId
			 *           用户Id
			 * @param selectedDate
			 *           选择的日期
			 * @return  List<SecScheduleDO>
		     */
			public List<SecScheduleDO> getDayRemiderList(Long  userId,Date selectedDate){
				
			List<SecScheduleDO>	list=this.getSecScheduleList(userId,selectedDate);
			System.out.println(" getDayRemiderList list.size="+list.size());
			//转换成Calendar
			Calendar selectedDateCal=Calendar.getInstance();
			selectedDateCal.setTime(selectedDate);
			//加一天
			Calendar addOneDay=Calendar.getInstance();
			addOneDay.setTime(selectedDate);
			addOneDay.add(Calendar.DATE, 1);
		    
			List<SecScheduleDO>	reminderList=new ArrayList<SecScheduleDO>(0);

		    //今日的提醒
			for(SecScheduleDO sch:list){
			     Calendar cal=Calendar.getInstance();
			     cal.setTime(sch.getStartTime());
			     cal.set(selectedDateCal.get(Calendar.YEAR), 
			    		 selectedDateCal.get(Calendar.MONTH), 
			    		 selectedDateCal.get(Calendar.DAY_OF_MONTH));
			     System.out.println(" calOne  "+cal.getTime());
				 SecScheduleDO newSecDO=null;
			     newSecDO=this.trackleRemindTime(cal,sch);
			     if(newSecDO!=null){
			    	 reminderList.add(newSecDO);
			     }
			}
			
			//明日的提醒
			List<SecScheduleDO>	tomorrowList=this.getSecScheduleList(userId,addOneDay.getTime());
			for(SecScheduleDO sch:tomorrowList){
			     Calendar calTwo=Calendar.getInstance();
			     calTwo.setTime(sch.getStartTime());
			     calTwo.set(selectedDateCal.get(Calendar.YEAR), 
			    		 selectedDateCal.get(Calendar.MONTH), 
			    		 selectedDateCal.get(Calendar.DAY_OF_MONTH));
			     calTwo.add(Calendar.DATE, 1);
			     System.out.println(" calTwo  "+calTwo.getTime());
				 SecScheduleDO newSecDO=null;
			     newSecDO=this.trackleRemindTime(calTwo,sch);
			     System.out.println(" newSecDO ="+newSecDO.getTimeGap());
			     if(newSecDO!=null){
			    	 reminderList.add(newSecDO);
			     }
			}
			
			System.out.println(" getDayRemiderList reminderList size"+reminderList.size());
			return reminderList;
			}

		
			
				/**
				 * 功能描述:处理该记录是否要加入提醒列表
				 * @param  cal
				 *           要提醒的日期(当天,或者第二天)
				 * @param  sch
				 *           提醒的记录    
				 * @return  SecScheduleDO 
			     */
		private SecScheduleDO  trackleRemindTime(Calendar  cal,SecScheduleDO sch){
		     Calendar nowCal=Calendar.getInstance();
		     nowCal.setTime(new Date());
			
		     //不提醒
		     if((cal.getTimeInMillis()<nowCal.getTimeInMillis())||sch.getRemindCycle().equals("25")){
		    	 return  null;
			     }
	
			//准时不处理
			//提前15分钟
		     if(sch.getRemindCycle().equals("27")){
			    cal.add(Calendar.MINUTE, -15);
		     }
	
			//提前30分钟
		     if(sch.getRemindCycle().equals("28")){
			    cal.add(Calendar.MINUTE, -30);
		     }
			//提前1小时
		     if(sch.getRemindCycle().equals("29")){
			    cal.add(Calendar.MINUTE, -60);
		     }
			//提前一天
		     if(sch.getRemindCycle().equals("30")){
				    cal.add(Calendar.DATE, -1);
			     }
			//时间差
		     Long 	 timeGap=(cal.getTimeInMillis()-nowCal.getTimeInMillis())/1000;
	       System.out.println(" timeGap service="+timeGap);
		  if(timeGap>0){
			  SecScheduleDO newDO=new SecScheduleDO();
			  newDO.setTitle(sch.getTitle());
			  newDO.setContent(sch.getContent());
			  newDO.setRemindTime(cal.getTime());
			  newDO.setTimeGap(timeGap);
			  newDO.setScheduleId(sch.getScheduleId());
			  newDO.setRemindCycle(sch.getRemindCycle());
			  newDO.setRepeatCycle(sch.getRepeatCycle());
			  newDO.setStartTime(sch.getStartTime());
			  newDO.setEndTime(sch.getEndTime());	
			  newDO.setUserId(sch.getUserId());
			  newDO.setEpId(sch.getEpId());
		   return  newDO;
		  }else{
		   return null;
		  }
		  
		}
	
}
